# -*- coding: UTF-8 -*-
import urllib
from urllib import request
import re
import pymysql
import os, sys, time, asyncio, aiohttp

from tqdm import tqdm


# 获取链接,下载文件
async def fetch(session: aiohttp.ClientSession, url: str, path: str, flag: str):
    async with session.get(url) as resp:
        html = await resp.text()
        # typelocation = re.search(r'安卓软件下载网</a> → <a href="/game/">安卓游戏</a>', html)
        # if typelocation != None:
        #     apkUrlList = re.findall('href="(http.*?\.apk)"', html)
        #     apkSizeList = re.findall('(\d+\.\d+)MB', html)
        #     appName = re.findall('<div class="a_app_attr_tit">\n<h1>(.*?)</h1>', html)
        #     path = "E:/app/apk/%s.apk" % (appName[0] if appName.__len__() > 0 else str(0))
        #     if apkSizeList.__len__() > 0:
        #         if apkUrlList.__len__() > 0 and (float(apkSizeList[0])) < 100.0:
        #             print('正在下载', apkUrlList[0])
        #             async with session.get(apkUrlList[0]) as resp:
        #                 with open(path, 'wb') as fd:
        #                     while 1:
        #                         chunk = await resp.content.read(1024)  # 每次获取1024字节
        #                         if not chunk:
        #                             break
        #                         fd.write(chunk)
    return ">>"


async def begin_download(sem, session: aiohttp.ClientSession, url: str, path: str, flag: str):
    # 控制协程并发数量
    with (await sem):
        return await fetch(session, url, path, flag)


async def download(sem: asyncio.Semaphore):
    tasks = []
    async with aiohttp.ClientSession() as session:
        for i in range(firstNo, 277999):
            print('抓取第', i, '页面')
            # 构造一个协程列表
            tasks.append(asyncio.ensure_future(
                begin_download(sem, session, "https://www.apk3.com/soft/%s.html" % i, "E:/app/apk/%s.apk"%i, i)))
        # 等待返回结果
        tasks_iter = asyncio.as_completed(tasks)
        # 创建一个进度条
        fk_task_iter = tqdm.tqdm(tasks_iter)
        for coroutine in fk_task_iter:
            # 获取结果
            res = await coroutine
            print(res, '下载完成')


# 查询上次最后一次的序号
def selectNo(cursor):
    cursor.execute("select *  from apk3 where _id=(select max(_id)  from apk3)")
    maxNO = cursor.fetchall()
    if maxNO.__len__() > 0:
        firstNo = maxNO[0][1]
        return firstNo
    return 0


# 插入序号
def installNO(cursor, db, NO, appName="", downloadTime="", TIME=0):
    sql = "insert into apk3(NO,appName, downloadTime, TIME) values (%s,%s,%s, %s)" % (NO, appName, downloadTime, TIME)
    cursor.execute(sql)
    db.commit()
    db.rollback()


# http://gyxz.hwm6b6.cn/a31/yx_wulan1/wxckfzjh.apk
# https://www.apk3.com/soft/277982.html,
# https://www.apk3.com/soft/281781.html
if __name__ == '__main__':
    db = pymysql.connect("localhost", "root", "164531", "appmsg")
    cursor = db.cursor()
    firstNo = selectNo(cursor)
    firstNo = 277982

    # 创建目录
    os.makedirs("E:/app/apk/", exist_ok=True)
    # 获取事件循环
    lp = asyncio.get_event_loop()
    # 创建一个信号量以防止DDos
    sem = asyncio.Semaphore(4)
    lp.run_until_complete(download(sem))
    print("-" * 50)
    print("任务加载完毕，等待下载完成！")
    lp.close()
